package com.yuyang.wifi.views.recyclerview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yuyang.wifi.R;
import com.yuyang.wifi.views.recyclerview.listener.DataIO;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 基类
 * Created by wislie on 2017/10/28.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> implements DataIO<T> {

    //用于设置要填充数据的标识
    protected static final int TYPE_ITEM = 0;
    //"亲,已经到底了呦~" 的标识
    protected static final int TYPE_FOOTER_NO_MORE = 1;
    //正在加载中 的标识
    protected static final int TYPE_FOOTER_LOADING = 2;

    protected List<T> mDataList;
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected int mItemLayoutId;

    //是否 没有更多
    protected boolean mIsNoMore;
    //是否 正在加载中
    protected boolean mIsLoading;

    public BaseAdapter(Context context, List<T> objects) {
        mDataList = objects;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<T> getObjects() {
        return mDataList;
    }

    public void setObjects(List<T> objects) {
        mDataList = objects;
    }

    public int getItemLayoutId() {
        return mItemLayoutId;
    }

    public void setItemLayoutId(int itemLayoutId) {
        mItemLayoutId = itemLayoutId;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER_NO_MORE) {
            View view = mInflater.from(parent.getContext()).inflate(R.layout.item_footer_no_more, null);
            return new NoDataFooterViewHolder(parent.getContext(), view);
        }

        /*else if(viewType == TYPE_FOOTER_LOADING){
            View view = mInflater.from(parent.getContext()).inflate(R.layout.item_footer_loading, null);
            return new LoadingFooterViewHolder(parent.getContext(), view);
        }*/

        return CommonViewHolder.get(mContext, parent, getItemLayoutId());
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {

            if (mIsNoMore) {
                return TYPE_FOOTER_NO_MORE;
            }

           /* else if(mIsLoading){
                return TYPE_FOOTER_LOADING;
            }*/
        }

        return TYPE_ITEM;
    }


    /*public void setIsNoMore(boolean isNoMore){
        this.mIsNoMore = isNoMore;
        notifyDataSetChanged();
    }*/


   /* public void setIsLoading(boolean isLoading){
        this.mIsLoading = isLoading;
        notifyDataSetChanged();
    }*/


    @Override
    public void onBindViewHolder(final CommonViewHolder holder, int position) {


    }


    @Override
    public int getItemCount() {
        if (mIsNoMore) {
            return mDataList.size() + 1;
        }
        return mDataList.size();
    }


    @Override
    public void add(T elem) {
        mDataList.add(elem);
        notifyDataSetChanged();
    }

    @Override
    public void addAt(int location, T elem) {
        mDataList.add(location, elem);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(List<T> elements) {
        mDataList.addAll(elements);
        notifyDataSetChanged();
    }

    @Override
    public void addAllAt(int location, List<T> elements) {
        mDataList.addAll(location, elements);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(boolean isFirstPage, List<T> elements) {
        mDataList.addAll(mDataList.size(), elements);
        if (isFirstPage) {
            notifyDataSetChanged();
        } else {
            notifyItemRangeChanged(mDataList.size(), mDataList.size() + elements.size());
        }
    }

    @Override
    public void remove(T elem) {
        mDataList.remove(elem);
        notifyDataSetChanged();
    }

    @Override
    public void removeAt(int index) {
        mDataList.remove(index);
        notifyDataSetChanged();
    }

    @Override
    public void removeAll(List<T> elements) {
        mDataList.removeAll(elements);
        notifyDataSetChanged();
    }

    @Override
    public void clear() {
        if (mDataList != null && mDataList.size() > 0) {
            mDataList.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public void replace(T oldElem, T newElem) {
        replaceAt(mDataList.indexOf(oldElem), newElem);
    }

    @Override
    public void replaceAt(int index, T elem) {
        mDataList.set(index, elem);
        notifyDataSetChanged();
    }

    @Override
    public void replaceAll(List<T> elements) {
        if (mDataList.size() > 0) {
            mDataList.clear();
        }
        mDataList.addAll(elements);
        notifyDataSetChanged();
    }

    @Override
    public T get(int position) {
        if (position >= mDataList.size())
            return null;
        return mDataList.get(position);
    }

    @Override
    public List<T> getAll() {
        return mDataList;
    }

    @Override
    public int getSize() {
        return mDataList.size();
    }

    @Override
    public boolean contains(T elem) {
        return mDataList.contains(elem);
    }

    @Override
    public void setData(List<T> elements) {
        mDataList = elements;
        notifyDataSetChanged();

    }

    public interface OnItemListener {

        //////////////////////  这3个和滑动的有关  ///////////////////////////
        //删除
        public void onDelete(int position);

        //取消
        public void onCancel(int position);

        public void onConfirm(int position);

        //item点击
        public void onItemClick(View view, int position);

        //item长按
        public void onItemLongClick(View view, int position);

        public void onMore(int position);

        public void onAddNum(int position, int num);

        public void onReduceNum(int position, int num);

        public void onaddNums(int position, int num);
    }

}
